import { open, close, write } from 'fs';
import { Buffer } from 'buffer';
const data1 = 'abcd';
const data2 = Buffer.from('abcd');

//必须开启写入权限
open('./files/app041.txt', 'w', (err, fd) => {
    if(err) {
    }else {
        write(fd, data1, (err, written, string) => {
            console.log('write 1', written);//4
            console.log('write 1', string);//abcd

            //打开的文件需要关闭
            close(fd, function () {});
        });
    }
});

//必须开启写入权限
open('./files/app042.txt', 'w', (err, fd) => {
    if(err) {
    }else {
        write(fd, data2, 0, 4, 0, function(err, bytesWritten, buffer) {
            console.log('write 2', bytesWritten);//4
            console.log('write 2', buffer.toString());//hello world

            //打开的文件需要关闭
            close(fd, function () {});
        });
    }
});